package com.comdo.newup_loan.module.register;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.bank.BankUtils;
import com.comdo.newup_loan.bank.bank.BankApiManager;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.comom.ProgressDialog;
import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.LoginInfo;
import com.comdo.newup_loan.util.TitleMenuUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import retrofit2.Response;


public class RegisterSuccessActivity extends BaseActivity<RegisterSuccessPresenter> implements RegisterSuccessContract.View{
	private TextView bankRegTextView;
	private TextView skipTextView;
	private String userName;
	private String passWord;


	@Override
	protected int getContentViewLayoutID() {
		return R.layout.activity_regsuccess;
	}

	@Override
	protected void init() {
		super.init();
	}

	@Override
	protected void initView(Bundle savedInstanceState) {
		new TitleMenuUtil(RegisterSuccessActivity.this, "",false);
		userName=getIntent().getStringExtra("userName");
		passWord=getIntent().getStringExtra("passWord");
		bankRegTextView = (TextView) findViewById(R.id.tv_bankreg);
		skipTextView = (TextView) findViewById(R.id.tv_skip);
		bankRegTextView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				// 跳往银行存管
			mPresenter.login(userName,passWord);
			}
		});
		skipTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// 暂不开通
				RegisterSuccessActivity.this.finish();
			}
		});
	}

	@Override
	public RegisterSuccessPresenter getmPresenter() {
		return new RegisterSuccessPresenter();
	}

	@Override
	public void presenterSetView() {
		if (mPresenter != null)
			mPresenter.setView(this);
	}

	@Override
	public void loginFail(String failMessage) {
		showToast(failMessage);
	}

	@Override
	public void getLoginInfo(LoginInfo loginInfo) {
		Config.loginInfo=loginInfo;
		CompositeDisposable mCompositeDisposable =new CompositeDisposable();
		ProgressDialog.show(RegisterSuccessActivity.this,false,"");
		FormBody body= BankApiManager.getIstance().personalTieCardRegister(RegisterSuccessActivity.this,Config.loginInfo.getPlatformUserNo());
		mCompositeDisposable.add(RetrofitHelper.createMyBankApi().getWebUrl(body)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Consumer<Response<String>>() {
					@Override
					public void accept(Response<String> Result) throws Exception  {
						RegisterSuccessActivity.this.finish();
						BankUtils.toWebView(Result.headers().get("Location"),RegisterSuccessActivity.this);
					}
				}, new Consumer<Throwable>() {
					@Override
					public void accept(Throwable throwable) throws Exception {;
						Toast.makeText(RegisterSuccessActivity.this,"请求失败",Toast.LENGTH_SHORT).show();
					}
				}));
	}
}
